/** 
*
* @author  jiwf 
* @date 创建时间：2016年10月24日 下午10:25:32 
* @version 1.0 
* @since  
*  
*/
package com.xbstar.web;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xbstar.bean.UserGroupRelation;
import com.xbstar.bean.UserOrder;
import com.xbstar.bean.UserToSessionRela;
import com.xbstar.dao.OrderRepository;
import com.xbstar.dao.UserGroupRelationRepository;
import com.xbstar.dao.UserToSessionRelaRepository;
import com.xbstar.tools.Timer;
import com.xbstar.tools.Utils;

@RestController
@RequestMapping("/userR2group")//用户关系
public class UserGroupRelationController {
	@Autowired
	UserGroupRelationRepository userGroupRelaRe;
	@Autowired
	UserToSessionRelaRepository user2SessRelaRe;
	@Autowired
	OrderRepository orderRe;
	@RequestMapping("/add")
	String addRelation(@RequestParam("userId")Long userId,HttpServletRequest req ){
		/**
		 * 1.假设groupRelation只能通过这个途径编化，但是sessionRelation可以通过其他途径变化
		 */
		JSONObject json=new JSONObject();
		UserGroupRelation ugr=new UserGroupRelation();
		ugr.setUserId(userId);
		ugr.setUserAddTime(Timer.getCurrentString());
		String belongTo=req.getParameter("groupBelongTo");
		if(belongTo==null){
			belongTo=Utils.getUUID();
		}
		ugr.setGroupBelongTo(belongTo);
		UserToSessionRela u2sr=new UserToSessionRela();
		u2sr.setJoinTime(ugr.getUserAddTime());
		u2sr.setSessionBelongTo(ugr.getGroupBelongTo());
		u2sr.setUserId(ugr.getUserId());
		//u2sr.setId(ugr.getId());
		user2SessRelaRe.save(u2sr);
		ugr.setId(u2sr.getId());
		ugr=userGroupRelaRe.save(ugr);
		json.put(UserPackage.STATUS, UserPackage.SUCCESS);
		JSONObject tjson=new JSONObject();
		tjson.put("groupBelongTo", ugr.getGroupBelongTo());
		tjson.put("id", ugr.getId());
		json.put(UserPackage.DATA, tjson);
		return json.toString();
	}
	@RequestMapping("/del")
	String delUserGroup(@RequestParam("id")Long id){
		JSONObject json=new JSONObject();
		try {
			user2SessRelaRe.delete(id);
			UserGroupRelation ugr =userGroupRelaRe.findOne(id);
			Long ordId=ugr.getOrderId();
			if(ordId!=null){
				UserOrder order=orderRe.findOne(ordId);
				if(order!=null){
					order.setState("1");
					orderRe.save(order);
				}
			}
			userGroupRelaRe.delete(id);
			json.put(UserPackage.STATUS, UserPackage.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			json.put(UserPackage.STATUS, UserPackage.EMPTY);
		}
		return json.toString();
	}
	@RequestMapping("/set")
	String setUserGroup(@RequestParam("user2GroupId")Long user2GroupId,@RequestParam("orderId")Long orderId){
		JSONObject json=new JSONObject();
		try {
			UserGroupRelation ugr= userGroupRelaRe.findOne(user2GroupId);
			UserOrder order=orderRe.findOne(orderId);
			if(ugr==null||order==null){
				json.put(UserPackage.STATUS, UserPackage.EMPTY);
			}else{
				ugr.setOrderId(orderId);
				order.setState("2");
				userGroupRelaRe.save(ugr);
				orderRe.save(order);
				json.put(UserPackage.STATUS, UserPackage.SUCCESS);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			json.put(UserPackage.STATUS, UserPackage.EMPTY);
		}
		return json.toString();
	}
}
